package servlet.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetCookieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		//클라이언트로 부터 받은 쿠키값들 조회해서 출력
		//쿠키값들 조회
		Cookie [] cs = request.getCookies();//전송된 쿠키가 하나도 없으면 null
		out.println("<html><body>");
		if(cs!=null){
			for(int i = 0; i<cs.length;i++){
			String name = cs[i].getName();
			String value = cs[i].getValue();
			if(name.equals("name")){
				value = URLDecoder.decode(cs[i].getValue(),"UTF-8");//Decoding
			}
			
			out.println(name+"-------"+value+"<br>");
		}
	}else{
		out.println("전송된 쿠키값이 없습니다.");
	}
		out.println("<a href='/myservlet/RemoveDateCookieServlet'>datTime 쿠키삭제+<a>");
		out.println("</body></html>");
	}
}

